/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.ORM;

import java.lang.reflect.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author gortiz
 */
public class ObjectoRelacionGenerador <T> {

    public ArrayList<T> generaObjetos(ResultSet rs, Class clase) throws ORMException{
        try {
          
            Field[] listaAtributos = clase.getDeclaredFields();     
            ArrayList<T> listaEntidad = new ArrayList();
            while(rs.next()){
                Object obj = clase.newInstance();
                
                for(Field atributo : listaAtributos){
                    Object datoActual = rs.getObject(atributo.getName());
                    Method metodo = clase.getMethod("set" + mayusculaPrimeraLetra(atributo.getName()), atributo.getType());
                    
                    metodo.invoke(obj, datoActual);
                         }
                listaEntidad.add((T) obj);
            }
           
            return listaEntidad;
        } catch (NoSuchMethodException ex) {
          
            throw new ORMException( );
        } catch (SecurityException ex) {
            throw new ORMException();
        }catch (IllegalArgumentException ex) {
            throw new ORMException();
        } catch (InvocationTargetException ex) {
            throw new ORMException();
        } catch (InstantiationException ex) {
            throw new ORMException();
        } catch (IllegalAccessException ex) {
            throw new ORMException();
        } catch (SQLException ex) {
            throw new ORMException();
        } 
    }

    private String mayusculaPrimeraLetra(String nombre){
            return nombre.substring(0,1).toUpperCase() + nombre.substring(1);

    }

}
